package com.the_ring.chapter05;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class PwdActivity extends AppCompatActivity {

    private EditText newPwdEt, confirmPwdEt, authCodeEt;
    private Button authCodeBt, confirmBt;
    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwd);

        String phone = getIntent().getStringExtra("phone");
        this.phone = phone;
        Log.i("PwdActivity", "phone: " + phone);

        initView();
    }

    private void initView() {
        newPwdEt = findViewById(R.id.et_new_pwd);
        confirmPwdEt = findViewById(R.id.et_confirm_pwd);
        authCodeEt = findViewById(R.id.et_auth_code);
        authCodeBt = findViewById(R.id.bt_auth_code);
        confirmBt = findViewById(R.id.bt_confirm);

        authCodeBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(PwdActivity.this)
                        .setTitle("请记住验证码")
                        .setMessage("手机号" + phone + "，本次登录验证码是151949，请输入验证码")
                        .setPositiveButton("好的", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .create()
                        .show();
            }
        });

        confirmBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!(newPwdEt.getText().toString().equals(confirmPwdEt.getText().toString()))) {
                    Toast.makeText(PwdActivity.this, "密码不一致！！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!(authCodeEt.getText().toString().equals("151949"))) {
                    Toast.makeText(PwdActivity.this, "验证码有误！！", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("pwd", newPwdEt.getText().toString());
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });

    }


}